package com.doit.day04

object _01_平均温度 {
  def main(args: Array[String]): Unit = {
    val d1 = Array(("beijing", 28.1), ("shanghai", 28.7), ("guangzhou", 32.0), ("shenzhen", 31.0))
    val d2 = Array(("beijing", 27.3), ("shanghai", 30.1), ("guangzhou", 33.3))
    val d3 = Array(("beijing", 28.2), ("shanghai", 29.1), ("guangzhou", 32.0), ("shenzhen", 32.0))

    //将所有得温度都放在一个集合里面 ==》 为什么？
    val tuples: Array[(String, Double)] = d3 ++ d1 ++ d2

    //按照城市进行分组
    val map: Map[String, Array[(String, Double)]] = tuples.groupBy(_._1)
    //Map[String,Array[(String,Double)]]
    val res: Map[String, Double] = map.map(tp => {
      //获取出来每个城市
      val city: String = tp._1
      //获取出来每个城市得所有得温度和城市
      val value: Array[(String, Double)] = tp._2
      //将value转换成数组里面全部放的是double类型得温度
      val temp: Array[Double] = value.map(_._2)
      //求平均温度
      val avgTemp: Double = (temp.sum / temp.size).formatted("%.2f").toDouble
      (city, avgTemp)
    })
    //打印  我想按照平均温度得降序进行排列
    //list[(String,Double)]
/*    res.toList.sorted(new Ordering[(String,Double)] {
      override def compare(x: (String, Double), y: (String, Double)) = y._2.compare(x._2)
    }).foreach(println)*/

//    res.toList.sortBy(_._2).reverse.foreach(println)

//    res.toList.sortWith(_._2 > _._2).foreach(println)




  }

}
